<?php
namespace appin\cms\taglib;

use think\template\TagLib;

class Cms extends TagLib
{
    protected $tags = [
        'load' => ['attr' => 'href,cache', 'close' => 0],
        'conf' => ['attr' => 'item,default,lang', 'close' => 0],
        'columns' => ['attr' => 'where,key,id,cache', 'close' => 1 ],
        'list' => ['attr' => 'field,mode,where,page,limit,order,key,id,cache', 'close' => 1],
        'adv' => ['attr' => 'where,page,limit,order,key,id,cache,lang', 'close' => 1]
    ];

    /**
     * 加载静态文件
     * @param {Object} $tag
     */
    public function tagLoad( $tag )
    {
        $href  = isset($tag['href']) ? explode(',' , $tag['href']) : [];
        $cache = isset($tag['cache']) ? ( (int) $tag['cache'] === 1 ? 1 : 0) : 0;
        $ver = '';
        if ($cache === 0) $ver = '?date='.date('YmdHi');
        $html = '';
        if ( ($total = count( $href ) ) > 0 ) {
            for ( $i = 0; $i < $total; $i++ ) {
                if ( stripos($href[$i], '.js') !== false ) {
                    $html .= "<script src='".$href[$i].$ver."' type='text/javascript'></script>\n";
                } elseif ( stripos($href[$i] , '.css') !== false ) {
                    $html .= "<link href='".$href[$i].$ver."' rel='stylesheet' />\n";
                } else {
                    $html .= "<?php include __DIR__ .'".$href[$i]."'; ?>";
                }
            }
        }
        return $html;
    }

    /**
     * 示例：{cms:conf item="webname" default="1111" lang="zh-cn"}
     * 配置信息
     * @param {Object} $tag
     */
    public function tagConf($tag)
    {
        $item 		= isset( $tag['item'] ) ? $tag['item'] : '';
        $default 	= isset( $tag['default'] ) ? $tag['default'] : '';
        $lang       = isset( $tag['lang'] ) ? $tag['lang'] : 0;

        return "<?php echo \appin\cms\model\Config::item(\"{$item}\", \"{$lang}\", \"{$default}\"); ?>";
    }

    /**
     * 获取指定栏目的子栏目
     * @param $tag
     * @param $content
     */
    public function tagColumns($tag, $content)
    {
        $condition  = isset($tag['where']) ? $tag['where'] : 'pid = 0 AND status = 1';
        $key        = isset($tag['key']) ? $tag['key'] : 'key';
        $id         = isset($tag['id']) ? $tag['id'] : 'value';
        $cache      = (isset($tag['cache']) && $tag['cache'] > 0) ? (int)$tag['cache'] : false;

        $parse  = '<?php $__LIST__ = \appin\cms\model\Column::lists("'.$condition.'",'.$cache.'); ?>';
        $parse .= '<?php if(!empty($__LIST__)): ?>';
        $parse .= '<?php foreach($__LIST__ as $'.$key.' => $'.$id.'): ?>';
        $parse .= $content;
        $parse .= '<?php endforeach; ?>';
        $parse .= '<?php endif; ?>';
        return $parse;
    }

    /**
     * @param $tag
     * @param $content
     * @return string
     */
    public function tagList($tag, $content)
    {
        $mode  = isset( $tag['mode'] ) ? $tag['mode'] : 'articles';
        $field = isset($tag['field']) ? $tag['field'] : '*';
        $where = isset( $tag['where'] ) ? $tag['where'] : '';
        $page  = isset( $tag['page'] ) ? (int) $tag['page'] : 1;
        $limit  = isset( $tag['limit'] ) ? (int) $tag['limit'] : 10;
        $order = isset( $tag['order'] ) ? $tag['order'] : '';
        $key = isset( $tag['key'] ) ? $tag['key'] : 'key';
        $id = isset( $tag['id'] ) ? $tag['id'] : 'value';
        $cache = (isset($tag['cache']) && $tag['cache'] > 0) ? (int)$tag['cache'] : false;

        $parse  = '<?php $__LIST__ = \appin\cms\model\\'.ucfirst($mode).'::lists("'.$field.'","'.$where.'",'.$page.','.$limit.',"'.$order.'",'.$cache.'); ?>';
        $parse .= '<?php if(!empty($__LIST__) ): ?>';
        $parse .= '<?php foreach($__LIST__ as $'.$key.' => $'.$id.'): ?>';
        $parse .= $content;
        $parse .= '<?php endforeach; ?>';
        $parse .= '<?php endif; ?>';
        return $parse;
    }

    /**
     * 获取广告标签
     * {cms:ad pos="1" lang="$lang" page="1" limit="10" order="id desc" key="key" id="ad" cache="0"}
     * {/cms:ad}
     * @param $tag
     * @param $content
     * @return string
     */
    public function tagAdv($tag, $content)
    {
        $where  = isset($tag['where']) ? $tag['where'] : '';
        $lang   = isset($tag['lang']) ? $tag['lang'] : 0;
        $page   = isset( $tag['page'] ) ? (int) $tag['page'] : 1;
        $limit  = isset( $tag['limit'] ) ? (int) $tag['limit'] : 10;
        $order  = isset( $tag['order'] ) ? $tag['order'] : '';
        $key    = isset( $tag['key'] ) ? $tag['key'] : 'key';
        $id     = isset( $tag['id'] ) ? $tag['id'] : 'value';
        $cache  = (isset($tag['cache']) && $tag['cache'] > 0) ? (int)$tag['cache'] : false;

        $parse  = '<?php $__LIST__ = \appin\cms\model\Adv::lists("'.$where.'","'.$lang.'",'.$page.','.$limit.',"'.$order.'",'.$cache.'); ?>';
        $parse .= '<?php if(!empty($__LIST__) ): ?>';
        $parse .= '<?php foreach($__LIST__ as $'.$key.' => $'.$id.'): ?>';
        $parse .= $content;
        $parse .= '<?php endforeach; ?>';
        $parse .= '<?php endif; ?>';
        return $parse;
    }
}